/*!
\copyright  Copyright (c) 2024 Qualcomm Technologies International, Ltd.
            All Rights Reserved.
            Qualcomm Technologies International, Ltd. Confidential and Proprietary.
\file
\brief      Helper functions for data blobs

*/

#include "data_blob_types.h"
#include <panic.h>
#include <stdlib.h>
#include <string.h>

data_blob_t DataBlob_Create(size_t size)
{
    data_blob_t blob = {0};
    if (size)
    {
        blob.data_length = size;
        blob.data = PanicUnlessMalloc(size);
    }
    return blob;
}

void DataBlob_Free(data_blob_t *blob)
{
    PanicNull(blob);
    free(blob->data);
    blob->data = NULL;
    blob->data_length = 0;
}

data_blob_t DataBlob_Concatenate(data_blob_t *blob_1, data_blob_t *blob_2)
{
    PanicNull(blob_1);
    PanicNull(blob_2);
    data_blob_t merged_blob = DataBlob_Create(blob_1->data_length + blob_2->data_length);
    uint8 *data = merged_blob.data;
    if (data)
    {
        memcpy(data, blob_1->data, blob_1->data_length);
        memcpy(&data[blob_1->data_length], blob_2->data, blob_2->data_length);
    }
    DataBlob_Free(blob_1);
    DataBlob_Free(blob_2);
    return merged_blob;
}

const_data_blob_t DataBlob_GetConst(data_blob_t blob)
{
    const_data_blob_t const_blob = {.data = blob.data, .data_length = blob.data_length};
    return const_blob;
}
